package hrms.service;


import hrms.dao.DepartmentMapper;
import hrms.dao.PostMapper;
import hrms.dao.StaffMapper;
import hrms.entity.Department;
import hrms.entity.Post;
import hrms.entity.Staff;
import hrms.util.MergeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    PostMapper postMapper;

    @Autowired
    StaffMapper staffMapper;

    public List<Department> selectAllDepartments(){
        return departmentMapper.selectAllDepartments();
    }
    public List<Department> selectDepartmentById(Integer departmentId){
        return departmentMapper.selectDepartmentById(departmentId);
    }

    public Department selectSingleDepartmentById(String id){
        return departmentMapper.selectSingleDepartmentById(id);
    }

    public Department selectDepartmentByName(String name){
        return departmentMapper.selectDepartmentByName(name);
    }

    public void addDepartment(String departmentName,Integer managerId){
        System.out.println("Department-service");
        departmentMapper.addDepartment(departmentName,managerId);
    }
    public void removeDepartment(Integer departmentId){
        int isSucceed = departmentMapper.removeDepartment(departmentId);
        if (isSucceed == 1) {
            System.out.println("部门删除成功！");
        } else {
            System.out.println("部门删除失败！");
        }
    }
    public void renameDepartment(Department department){
        departmentMapper.renameDepartment(department);
    }
    public void changeCurrentNum(Integer departmentId,Integer num){
        int isSucceed = departmentMapper.changeCurrentNum(departmentId,num);
        if (isSucceed == 1) {
            System.out.println("部门人数修改成功！");
        } else {
            System.out.println("部门人数修改失败！");
        }
    }
    public void changeManager(Integer departmentId,Integer managerId){
        //由于department表还没有设置manager的外键约束，所以这里还没有处理一致性。
        int isSucceed = departmentMapper.changeManager(departmentId, managerId);
        if (isSucceed == 1) {
            System.out.println("部门经理变更成功！");
        } else {
            System.out.println("部门经理变更失败！");
        }
    }

    public void mergeDepartment(MergeUtil mergeUtil){
        // 新部门
        Department departmentForBase = departmentMapper.selectDepartmentByName(mergeUtil.getDept1());
        // 需要合并的部门
        Department department = departmentMapper.selectDepartmentByName(mergeUtil.getDept2());
        Integer departmentId = Integer.valueOf(department.getId());
        // 更新部门人数
        departmentForBase.setCurNum(departmentForBase.getCurNum() + department.getCurNum());
        departmentMapper.renameDepartment(departmentForBase);
        // 更新岗位所在部门
        List<Post> posts = postMapper.selectByDepartmentId(departmentId);
        for(Post post:posts){
            post.setDepartment(departmentForBase);
            postMapper.updateByPrimaryKey(post);
        }

        // 更新员工所在部门
        List<Staff> staffList = staffMapper.findByDept(departmentId);
        System.out.println(departmentId);
        System.out.println(staffList);
        for(Staff staff:staffList){
            staff.setDepartment(departmentForBase);
            staffMapper.updateByPrimaryKey(staff);
        }
        removeDepartment(departmentId);
    }
}
